﻿using Engine.Graphics;
using ModelViewer;
using Scenario_Editor.AddinManager;
using SharpDX;
using System;
using System.Windows.Forms;

namespace ModelViewerRoot.TabsForModelviewer
{
    public partial class PaintMaterial : UserControl
    {
        #region Private Fields

        private Engine.Engine engine;
        private MaterialManager materialManager;
        private object materialPainter1;
        private SelectedObject model;
        private ITerrainPaintingControl paintControl;

        #endregion Private Fields

        #region Public Constructors

        public PaintMaterial()
        {
            InitializeComponent();
        }

        #endregion Public Constructors

        #region Public Methods

        public void SaveCurrentMaterialToXML(string filename)
        {
        }

        #endregion Public Methods

        #region Internal Methods

        internal void OnAddinChange(object sender, EventArgs e)
        {
            Scenario_Editor.AddinManager.IAddinScenarioEditor addin = (Scenario_Editor.AddinManager.IAddinScenarioEditor)sender;

            // Remove old paintcontrol
            if (this.paintControl != null)
            {
                this.splitContainer1.Panel1.Controls.Remove(((Control)this.paintControl));
            }

            // Add Control
            this.paintControl = addin.TerrainPaintingControl;
            this.paintControl.MaterialManagerChanged(this.materialManager, null);
            Control control = (Control)this.paintControl;
            control.Dock = DockStyle.Fill;
            this.splitContainer1.Panel1.Controls.Add((Control)this.paintControl);
        }

        internal void SetGraphicEngine(Engine.Engine engine)
        {
            this.engine = engine;
        }

        internal void SetMaterial(IMaterial material)
        {
        }

        internal void SetMaterialManager(MaterialManager materialManager)
        {
            this.materialManager = materialManager;
        }

        internal void SetModel(SelectedObject model)
        {
            this.model = model;
        }

        internal void UseTool()
        {
            if (this.model != null)
                if (this.model.ScenerieObject.Mesh != null)
                {
                    Vector3 brushPosition;
                    int materialID;
                    int intersectedPolygon;
                    this.model.ScenerieObject.Mesh.Intersect(this.engine.Grafic.VNear, this.engine.Grafic.VFar, out brushPosition, out materialID, out intersectedPolygon);

                    if (this.paintControl.PaintVertexData)
                    {
                        if (brushPosition != Vector3.Zero && this.engine.Mouse.ButtonL)
                        {
                            //this.model.ScenerieObject.Mesh.PaintVertexColor(brushPosition, materialID, this.materialPainter1.VertexColor, 30);
                            this.model.ScenerieObject.Mesh.PaintNearestVertex(brushPosition, intersectedPolygon, this.paintControl.VertexData);
                        }
                        //this.label_MaterialID.Text = string.Format("Material: {0}", materialID);
                    }

                    if (this.paintControl.PaintMaterial)
                    {
                        if (brushPosition != Vector3.Zero && this.engine.Mouse.ButtonL)
                        {
                            this.model.ScenerieObject.Mesh.PaintMaterial(intersectedPolygon, this.paintControl.GetCurrentTerrainMaterial());
                        }
                    }
                }
        }

        #endregion Internal Methods
    }
}